package com.pri.chat.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.netease.nim.uikit.net.StatusBarUtil;
import com.netease.nim.uikit.net.entity.DTBean;
import com.netease.nim.uikit.net.entity.WishListBean;
import com.pri.chat.MainActivity;
import com.pri.chat.R;
import com.pri.chat.fragment.MainFragment;
import com.pri.chat.fragment.MemberDTFragment;
import com.pri.chat.fragment.MemberVideoFragment;
import com.pri.chat.fragment.VideoFragment1;
import com.pri.chat.fragment.WishAllFragment;
import com.pri.chat.fragment.WishFragment;
import com.vondear.rxtool.RxActivityTool;

import java.util.ArrayList;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 描述：添加类的描述
 *
 * @author
 * @time 2020/4/27
 */
public class DongTaiAndVideoActivity extends AppCompatActivity {
    @BindView(R.id.rl_center)
    RelativeLayout rlCenter;
    @BindView(R.id.vp_wish)
    ViewPager vpWish;
    @BindView(R.id.iv_return)
    ImageView ivBack;
    @BindView(R.id.iv_release)
    ImageView ivRelease;
    @BindView(R.id.stl_wish)
    SlidingTabLayout stlWish;
    private String mOtherId;
    private String mOtherName;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dongtaiandvideo_layout);
        View view = findViewById(R.id.rl_center);
        StatusBarUtil.setColor(this, getResources().getColor(R.color.white), 0);
        StatusBarUtil.setTranslucentForImageViewInFragment(this, 0, view);
        StatusBarUtil.setLightMode(this);
        ButterKnife.bind(this);
        Bundle bundle = getIntent().getExtras();
        mOtherId = bundle.getString("id");
        mOtherName = bundle.getString("name");

        initView();

        IntentFilter filter = new IntentFilter();
        filter.addAction(MainActivity.UPDATE_DACALL);
        filter.addAction(MainActivity.UPDATE_DONGTAI);
        filter.addAction(MainActivity.DELETE_DONGTAI);
        filter.addAction(MainActivity.DELETE_VEDIO);
        filter.addAction(MainActivity.UPDATE_VEDIO);

        myReceiver = new MyReceiver();
        registerReceiver(myReceiver, filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.unregisterReceiver(myReceiver);
    }

    private MyReceiver myReceiver;
    public class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(MainActivity.UPDATE_DACALL.equals(intent.getAction())){
                //更新动态
                String memberId = intent.getExtras().getString("memberId");
                int sfdc = intent.getExtras().getInt("sfdc");
                updateDaCall(memberId,sfdc);
            }else if(MainActivity.UPDATE_DONGTAI.equals(intent.getAction())){
                DTBean dtBean = intent.getExtras().getParcelable("dtObj");
                updateDongTai(dtBean);
            }else if(MainActivity.DELETE_DONGTAI.equals(intent.getAction())){
                DTBean dtBean = intent.getExtras().getParcelable("dtObj");
                if(arrayList.get(0) != null){
                    ((MemberDTFragment)arrayList.get(0)).deleteDongTai(dtBean);
                }
            }else if(MainActivity.DELETE_VEDIO.equals(intent.getAction())){
                DTBean dtBean = intent.getExtras().getParcelable("dtObj");
                if(arrayList.get(1) != null){
                    ((MemberVideoFragment)arrayList.get(1)).deleteVedio(dtBean);
                }
            }else if(MainActivity.UPDATE_VEDIO.equals(intent.getAction())){
                DTBean dtBean = intent.getExtras().getParcelable("dtObj");
                if(arrayList.get(1) != null){
                    ((MemberVideoFragment)arrayList.get(1)).updateVedio(dtBean);
                }
            }
        }
    }

    public void updateDaCall(String memberId, int sfdc){
        if(arrayList.get(0) != null){
            ((MemberDTFragment)arrayList.get(0)).updateDaCall(memberId,sfdc);
        }
    }

    public void updateDongTai(DTBean dtBean){
        if(arrayList.get(0) != null){
            ((MemberDTFragment)arrayList.get(0)).updateDongTai(dtBean);
        }
    }



    private String[] mTitle = new String[]{"动态"};


    private int selectIndex = 0;
    private ArrayList<Fragment> arrayList = new ArrayList<>();
    private void initView() {
        arrayList.add(MemberDTFragment.getInstance(mOtherId,mOtherName));
//        arrayList.add(MemberVideoFragment.getInstance(mOtherId,mOtherName));
        stlWish.setViewPager(vpWish, mTitle, this, arrayList);
        stlWish.getTitleView(0).setTextSize(20);
//        stlWish.getTitleView(1).setTextSize(14);
        stlWish.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                selectIndex = position;
                for (int i = 0; i < arrayList.size(); i++) {
                    if (i == position) {
                        stlWish.getTitleView(position).setTextSize(20);
                        stlWish.getTitleView(position).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        stlWish.getTitleView(i).setTextSize(14);
                        stlWish.getTitleView(i).setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }

            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        vpWish.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectIndex = position;
                for (int i = 0; i < arrayList.size(); i++) {
                    if (i == position) {
                        stlWish.getTitleView(position).setTextSize(20);
                        stlWish.getTitleView(position).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        stlWish.getTitleView(i).setTextSize(14);
                        stlWish.getTitleView(i).setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        stlWish.getTitleView(selectIndex).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    }


    @OnClick({R.id.iv_return, R.id.iv_release})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_return:
                finish();
                break;
            case R.id.iv_release:
                if(selectIndex == 0){
                    RxActivityTool.skipActivity(this, ReleaseDTActivity.class);
                }
//                else{
//                    RxActivityTool.skipActivity(this, ReleaseVideoActivity.class);
//                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK){
            if(requestCode == 1){
                arrayList.get(0).onActivityResult(10,resultCode,data);
            }
        }
    }
}
